﻿Option Strict On
Imports System.Text
Public Class debuggingform
    Public RTFGenerator As New RTFGenerator
    Public KnownIssues As String
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Not RTFGenerator.IsLoaded Then Exit Sub
        Dim LineCount As Integer = CInt(Val(TextBox1.Text))
        If LineCount < 25 Then LineCount = 25 : TextBox1.Text = "25"
        '  RichTextBox1.Rtf = RTFGenerator.TESTRTF(LineCount)
        RichTextBox1.Text = "Generating Test Code..."
        Dim TestSourceCode As String = RTFGenerator.GenerateSpeedTestClass(LineCount)
        RichTextBox1.Text = "Test Code Generated... Generating RTF..."
        RichTextBox1.Update()
        Dim SW As Stopwatch = Stopwatch.StartNew
        RichTextBox1.Rtf = RTFGenerator.GetHighlightRTF(TestSourceCode)
        Dim Elapsedticks As Long = SW.Elapsed.Ticks
        SW.Stop()
        RichTextBox2.Text = RichTextBox1.Rtf
        Dim TS As New TimeSpan(Elapsedticks)
        Label3.Text = TS.TotalMilliseconds.ToString & " milliseconds"
        'Subtract2, because the first line in the list is a non-text line, and the last line is the rtf closing end tag
        Label4.Text = "Available lines: " & (RTFLines(RichTextBox1).Count - 2).ToString
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        KnownIssues = My.Resources.KnownIssues
        RichTextBox2.Text = KnownIssues
        '    Me.Show()
        '   Me.Update()
    End Sub
    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not Char.IsDigit(e.KeyChar) Then
            If Not e.KeyChar = vbBack Then
                If Not e.KeyChar = Chr(Keys.Delete) Then
                    e.Handled = True
                End If
            End If
        End If
    End Sub
    Public Function RTFLines(RichTextBox As RichTextBox) As List(Of String)
        Dim RTFSource As String = RichTextBox.Rtf.Replace(vbLf, vbCr)
        RTFSource = RTFSource.Replace(vbCr & vbCr, vbCr)
        RTFSource = RTFSource.Replace(vbCr, "")
        RTFSource = RTFSource.Replace(vbLf, "")
        '   RTFSource = RTFSource.Replace(vbCrLf, "")

        RTFSource = RTFSource.Replace("\par ", vbCr)
        RTFSource = RTFSource.Replace("\par", vbCr)
        Dim Lines As List(Of String) = RTFSource.Split({vbCr}, 9999, StringSplitOptions.RemoveEmptyEntries).ToList
        Dim Result As New List(Of String)
        For Each Line As String In Lines
            Result.Add(Line & "\par")
        Next
        Result(Result.Count - 1) = Replace(Result(Result.Count - 1), "\par ", "")
        Return Result
    End Function

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim Lines As List(Of String) = RTFLines(RichTextBox1)
        Dim MaxLineNumber As Integer = Lines.Count - 2
        Dim Num As Integer = CInt(Val(TextBox2.Text))
        If Num = 0 Then showErrormsg() : Exit Sub
        If Num > MaxLineNumber Then showErrormsg() : Exit Sub
        RichTextBox2.Text = RichTextBox1.Rtf
        'Dim Location As Integer = RichTextBox2.Text.IndexOf(Lines(Num))
        'Dim Length As Integer = Lines(Num).Length
        'RichTextBox2.SelectionStart = Location
        'RichTextBox2.SelectionLength = Length
        'RichTextBox2.SelectionBackColor = Color.LightSkyBlue
        'RichTextBox2.SelectionColor = Color.Black
        'RichTextBox2.SelectionLength = 0
        '       MessageBox.Show("Line#" & Num & ":" & vbCrLf & vbCrLf & Lines(Num), "RichTextBox Line RTF", MessageBoxButtons.OK, MessageBoxIcon.Information)
        RichTextBox3.ForeColor = Color.Peru
        RichTextBox3.WordWrap = False
        RichTextBox3.Text = "Line#" & Num & ":" & vbCrLf & Lines(Num)
    End Sub
    Sub showErrormsg()
        MessageBox.Show("Please ensure that requested line exists.", "Line not found!", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        RichTextBox3.ForeColor = Color.RosyBrown
        RichTextBox3.WordWrap = True
        RichTextBox3.Text = "Header:" & vbCrLf & RTFLines(RichTextBox1)(0)
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        RichTextBox2.Text = KnownIssues
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        RichTextBox2.Text = RichTextBox1.Rtf
    End Sub
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        TestEditor.Show()
    End Sub
End Class
